#!/bin/bash
CC=gcc
CXX=g++
CLANG=clang
OPT=-O3
LDFLAGS=-rdynamic
CFLAGS=-g
#INSTRUCTION_TRACING="-DINSTRUCTION_TRACING -DINSTRUCTION_TRACING_DBT"
INSTRUCTIONIR="-DINSTRUCTIONIR"
#DEBUG="-DDEBUG -DDEBUGCONSOLE"
if [ "`uname -s`" == "Darwin" ]
then
    ASMUTILITIES=""
else
    ASMUTILITIES="-DASMUTILITIES"
fi
#ASMUTILITIES=""
ASMOBJ=""

echo "Compiling core emulator files..."
$CC $OPT -c 6502.c $DEBUG $INSTRUCTION_TRACING $CFLAGS $LDFLAGS
$CC $OPT -c romLoader.c $DEBUG  $CFLAGS $LDFLAGS
$CC $OPT -c NESRAM.c -fPIC $LDFLAGS

$CC $OPT -c NESMemory.c $DEBUG $CFLAGS $LDFLAGS
#$CC $OPT -fPIC -shared -o NESMemory.so NESMemory.c NESRAM.o $LDFLAGS

$CC $OPT -c PPU.c $DEBUG $CFLAGS $LDFLAGS
$CC $OPT -c PPUMemory.c $ASMUTILITIES $DEBUG $CFLAGS $LDFLAGS
$CC $OPT -c main.c $ASMUTILITIES $DEBUG $CFLAGS $LDFLAGS
$CC $OPT -c NES.c $DEBUG $CFLAGS $LDFLAGS
$CC $OPT -c KeyboardJoypad.c $CFLAGS $LDFLAGS
$CC $OPT -c Input.c $CFLAGS $LDFLAGS
$CC $OPT -c Movie.c $CFLAGS $LDFLAGS
$CC $OPT -c 6502_Debug.c $CFLAGS $LDFLAGS
$CC $OPT -c 6502_Dis.c $CFLAGS $LDFLAGS
$CC $OPT -c List.c $CFLAGS $LDFLAGS
$CC $OPT -c Statistics.c $CFLAGS $LDFLAGS

cpp -CC $INSTRUCTION_TRACING -D_6502_MACRO_GEN 6502.c > 6502.pp
./generate6502Macros.php > 6502_InstructionMacros.h

cpp -DINSTRUCTIONIR 6502.c > 6502_bare.c
#gcc -fPIC -O3 -fuse-linker-plugin -flto -c 6502_bare.c -o 6502_bare.o
gcc -fPIC -fverbose-asm -c 6502_bare.c -o 6502_bare.o $CFLAGS

#$CLANG -O0 -c 6502.c -o 6502.bc $DEBUG $INSTRUCTION_TRACING $INSTRUCTIONIR $CFLAGS -emit-llvm
#opt 6502.bc -o 6502.bc -strip-debug -always-inline

echo "Compiling SDL frontend..."
$CC $OPT -c SDL/SDL.c $DEBUG $CFLAGS $LDFLAGS
$CC $OPT -c SDL/Video.c $DEBUG $CFLAGS $LDFLAGS
$CC $OPT -c SDL/Keyboard.c $CFLAGS $LDFLAGS

echo "Compiling DBT..."
$CC $OPT -c DBT/DBT.c $CFLAGS $LDFLAGS

echo "Compiling DBT data structures..."
$CC $OPT -c DBT/BMC.c $CFLAGS $LDFLAGS
$CC $OPT -c DBT/BMCRepository.c $ASMUTILITIES $CFLAGS $LDFLAGS
$CC $OPT -c DBT/BMCIR.c $CFLAGS $LDFLAGS
$CC $OPT -c DBT/HMC.c $CFLAGS $LDFLAGS
#$CXX $OPT -c DBT/BMCIR.cpp $CFLAGS `llvm-config --cppflags` $LDFLAGS
#$CXX $OPT -c DBT/HMC.cpp $CFLAGS `llvm-config --cppflags` $LDFLAGS

echo "Compiling DBT Context data structures..."
$CC $OPT -c DBT/Context/ExeContext.c $CFLAGS $LDFLAGS
$CC $OPT -c DBT/Context/ExeContextRepository.c $CFLAGS $LDFLAGS

echo "Compiling DBT Scanning..."
$CC $OPT -c DBT/Scanning/Scanning.c $CFLAGS $LDFLAGS
$CC $OPT -c DBT/Scanning/Scanner.c $CFLAGS $LDFLAGS

echo "Compiling DBT Execution..."
$CC $OPT -c DBT/Execution/Execution.c $CFLAGS $LDFLAGS
$CC $OPT -c DBT/Execution/EE.c $CFLAGS $DEBUG $LDFLAGS

echo "Compiling DBT Translation..."
$CC $OPT -c DBT/Translation/Translation.c $CFLAGS $LDFLAGS
$CC $OPT -c DBT/Translation/BMCIStringSynth.c $CFLAGS $LDFLAGS
$CC $OPT -c DBT/Translation/JIT.c $CFLAGS $LDFLAGS
$CC $OPT -c DBT/Translation/ParallelCompiler.c $CFLAGS $LDFLAGS
#$CXX $OPT -c DBT/Translation/JIT.cpp $CFLAGS `llvm-config --cppflags` $LDFLAGS

if [ "$ASMUTILITIES" != "" ]; then
    echo "Assembling x86_64 utilities for Linux..."
    as ASMUtilities.asm -o ASMUtilities.o --gstabs
    ASMOBJ="ASMUtilities.o"
fi


if [ "`uname -s`" == "Darwin" ]
then
    if [ ! -d "/tmp/sdl12" ]
    then
        (
            echo "Downloading and building libSDL 1.2.15 without Cocoa support..."
            curl http://www.libsdl.org/release/SDL-1.2.15.tar.gz -o /tmp/SDL-1.2.15.tar.gz
            cd /tmp
            tar xzf SDL-1.2.15.tar.gz
            cd SDL-1.2.15
            ./configure --with-x --enable-video-carbon=no --enable-video-cocoa=no --prefix=/tmp/sdl12 --enable-video-x11=yes --enable-audio=no --enable-cdrom=no --enable-joystick=no
            make
            make install
            echo "Done."
        )
    fi
    echo "Linking..."
    $CC $OPT -o jrynes main.o 6502.o $ASMOBJ ExeContextRepository.o Statistics.o HMC.o ParallelCompiler.o ExeContext.o BMCIR.o JIT.o List.o NESRAM.o Translation.o BMCIStringSynth.o Execution.o EE.o Scanning.o BMC.o Scanner.o BMCRepository.o DBT.o romLoader.o NESMemory.o PPU.o PPUMemory.o NES.o SDL.o Video.o Keyboard.o KeyboardJoypad.o Input.o 6502_Debug.o Movie.o 6502_Dis.o $(/tmp/sdl12/bin/sdl-config --cflags) $(/tmp/sdl12/bin/sdl-config --libs) $(pkg-config --cflags --libs x11) -rdynamic -ldl $CFLAGS
else
    echo "Linking..."
    $CC $OPT -o jrynes main.o 6502.o $ASMOBJ ExeContextRepository.o Statistics.o HMC.o ParallelCompiler.o ExeContext.o BMCIR.o JIT.o List.o NESRAM.o Translation.o BMCIStringSynth.o Execution.o EE.o Scanning.o BMC.o Scanner.o BMCRepository.o DBT.o romLoader.o NESMemory.o PPU.o PPUMemory.o NES.o SDL.o Video.o Keyboard.o KeyboardJoypad.o Input.o 6502_Debug.o Movie.o 6502_Dis.o $(sdl-config --cflags) $(sdl-config --libs) -rdynamic -ldl $CFLAGS
fi

echo "Done."
